package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.logistic.domain.*;
import com.ruoyi.logistic.domain.vo.NetworkUndertakeGoRankVo;
import com.ruoyi.logistic.domain.vo.NetworkUndertakeGoVo;
import com.ruoyi.logistic.mapper.*;
import com.ruoyi.logistic.service.NetworkUndertakeGoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class NetworkUndertakeGoServiceImpl implements NetworkUndertakeGoService {
    @Autowired
    private NetworkUndertakeGoDetailMapper networkUndertakeGoDetailMapper;
    @Autowired
    private NetworkUndertakeGoCollectMapper networkUndertakeGoCollectMapper;
    @Autowired
    private NetworkUndertakeGoProvinceIncomeRankMapper networkUndertakeGoProvinceIncomeRankMapper;
    @Autowired
    private NetworkUndertakeGoSettleObjectNameIncomeRankMapper networkUndertakeGoSettleObjectNameIncomeRankMapper;
    @Autowired
    private NetworkNameMapper networkNameMapper;
    @Autowired
    private NetworkSettleTypeMapper networkSettleTypeMapper;
    @Override
    public List<NetworkUndertakeGoDetail> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        List<NetworkUndertakeGoDetail> details = networkUndertakeGoDetailMapper.selectAll();
        for (NetworkUndertakeGoDetail detail : details){
            detail.setNetworkName(networkNameMapper.selectNameById(detail.getNetworkNameId()));
            detail.setAccountType(networkSettleTypeMapper.selectNameById(detail.getSettleTypeId()));
            detail.setSettleObjectName(networkNameMapper.selectNameById(detail.getSettleObjectNameId()));
            detail.setReceiveSiteName(networkNameMapper.selectNameById(detail.getReceiveSiteNameId()));
        }
        return details;
    }

    @Override
    public Boolean insert(NetworkUndertakeGoDetail networkUndertakeGoDetail) {
        return networkUndertakeGoDetailMapper.insert(networkUndertakeGoDetail);
    }

    @Override
    public Boolean update(NetworkUndertakeGoDetail networkUndertakeGoDetail) {
        return networkUndertakeGoDetailMapper.update(networkUndertakeGoDetail);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return networkUndertakeGoDetailMapper.deleteById(id);
    }

    @Override
    public List<NetworkUndertakeGoDetail> search(NetworkUndertakeGoVo networkUndertakeGoVo) {
        PageHelper.startPage(networkUndertakeGoVo.getPageDate());
        List<NetworkUndertakeGoDetail> details = networkUndertakeGoDetailMapper.search(networkUndertakeGoVo);
        for (NetworkUndertakeGoDetail detail : details){
            detail.setNetworkName(networkNameMapper.selectNameById(detail.getNetworkNameId()));
            detail.setAccountType(networkSettleTypeMapper.selectNameById(detail.getSettleTypeId()));
            detail.setSettleObjectName(networkNameMapper.selectNameById(detail.getSettleObjectNameId()));
            detail.setReceiveSiteName(networkNameMapper.selectNameById(detail.getReceiveSiteNameId()));
        }
        return details;
    }

    @Override
    public List<NetworkUndertakeGoCollect> collectUndertakeGo(NetworkUndertakeGoVo networkUndertakeGoVo) {
        PageHelper.startPage(networkUndertakeGoVo.getPageDate());
        List<NetworkUndertakeGoCollect> list = networkUndertakeGoCollectMapper.collectUndertakeGo(networkUndertakeGoVo);
        for (NetworkUndertakeGoCollect detail : list){
            detail.setNetworkName(networkNameMapper.selectNameById(detail.getNetworkNameId()));
            detail.setAccountType(networkSettleTypeMapper.selectNameById(detail.getSettleTypeId()));
            detail.setSettleObjectName(networkNameMapper.selectNameById(detail.getSettleObjectNameId()));
        }
        return list;
    }

    @Override
    public List<NetworkUndertakeGoProvinceIncomeRank> provinceIncomeRank(NetworkUndertakeGoRankVo networkUndertakeGoRankVo) {
        return networkUndertakeGoProvinceIncomeRankMapper.provinceIncomeRank(networkUndertakeGoRankVo);
    }

    @Override
    public NetworkUndertakeGoSettleObjectNameIncomeRankAll settleObjectNameIncomeRank(NetworkUndertakeGoRankVo networkUndertakeGoRankVo) {
        List<NetworkUndertakeGoSettleObjectNameIncomeRank> ranks = networkUndertakeGoSettleObjectNameIncomeRankMapper.settleObjectNameIncomeRank(networkUndertakeGoRankVo);
        List<String> networkNameList = new ArrayList<>();
        List<BigDecimal> incomeList = new ArrayList<>();

        for (NetworkUndertakeGoSettleObjectNameIncomeRank obj : ranks) {
            networkNameList.add(obj.getSettleObjectName());
            incomeList.add(obj.getIncome());
        }

// 然后将集合转换为数组
        String[] networkNames = networkNameList.toArray(new String[0]);
        BigDecimal[] incomes = incomeList.toArray(new BigDecimal[0]);

// 创建并初始化新对象
        NetworkUndertakeGoSettleObjectNameIncomeRankAll all = new NetworkUndertakeGoSettleObjectNameIncomeRankAll();
        all.setNetworkName(networkNames);
        all.setIncome(incomes);
        return all;
    }
}
